//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FixedRateCoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/InterestRate.h>
#include <gen/QL/Cashflows/Coupon.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (DateTime paymentDate, Double nominal, Cephei::QL::IInterestRate^ interestRate, DateTime accrualStartDate, DateTime accrualEndDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd) : CCoupon(CFixedRateCoupon::typeid)
{
    CInterestRate^ _CinterestRate;
    try
    {
#ifdef HANDLE
        _phFixedRateCoupon = NULL;
#endif
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate);
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal);
        _CinterestRate = safe_cast<CInterestRate^> (interestRate);
        _CinterestRate->Lock();
        QuantLib::InterestRate& _interestRate = static_cast<QuantLib::InterestRate&> (_CinterestRate->GetReference ()); 
        QuantLib::Date _accrualStartDate = (QuantLib::Date)ValueHelper::Convert (accrualStartDate);
        QuantLib::Date _accrualEndDate = (QuantLib::Date)ValueHelper::Convert (accrualEndDate);
        QuantLib::Date _refPeriodStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodStart) ? (QuantLib::Date)ValueHelper::Convert (refPeriodStart->Value) : QuantLib::Date()); //4
        QuantLib::Date _refPeriodEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodEnd) ? (QuantLib::Date)ValueHelper::Convert (refPeriodEnd->Value) : QuantLib::Date()); //4
        _ppFixedRateCoupon = new boost::shared_ptr<QuantLib::FixedRateCoupon> (new QuantLib::FixedRateCoupon ( _paymentDate,  _nominal,  _interestRate,  _accrualStartDate,  _accrualEndDate,  _refPeriodStart,  _refPeriodEnd ));
        SetCoupon (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppFixedRateCoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CinterestRate != nullptr) _CinterestRate->Unlock();
    }
}
Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (DateTime paymentDate, Double nominal, Double rate, Cephei::QL::Times::IDayCounter^ dayCounter, DateTime accrualStartDate, DateTime accrualEndDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd) : CCoupon(CFixedRateCoupon::typeid)
{
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFixedRateCoupon = NULL;
#endif
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate);
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal);
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Date _accrualStartDate = (QuantLib::Date)ValueHelper::Convert (accrualStartDate);
        QuantLib::Date _accrualEndDate = (QuantLib::Date)ValueHelper::Convert (accrualEndDate);
        QuantLib::Date _refPeriodStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodStart) ? (QuantLib::Date)ValueHelper::Convert (refPeriodStart->Value) : QuantLib::Date()); //4
        QuantLib::Date _refPeriodEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodEnd) ? (QuantLib::Date)ValueHelper::Convert (refPeriodEnd->Value) : QuantLib::Date()); //4
        _ppFixedRateCoupon = new boost::shared_ptr<QuantLib::FixedRateCoupon> (new QuantLib::FixedRateCoupon ( _paymentDate,  _nominal,  _rate,  _dayCounter,  _accrualStartDate,  _accrualEndDate,  _refPeriodStart,  _refPeriodEnd ));
        SetCoupon (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppFixedRateCoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (boost::shared_ptr<QuantLib::FixedRateCoupon>& childNative, Object^ owner) : CCoupon(CFixedRateCoupon::typeid)
{
#ifdef HANDLE
	_phFixedRateCoupon = NULL;
#endif
	_ppFixedRateCoupon = &childNative;
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppFixedRateCoupon));
}
Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (QuantLib::FixedRateCoupon& childNative, Object^ owner) : CCoupon(CFixedRateCoupon::typeid)
{
#ifdef HANDLE
	_phFixedRateCoupon = NULL;
#endif
	_ppFixedRateCoupon = new boost::shared_ptr<QuantLib::FixedRateCoupon> (&childNative);
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppFixedRateCoupon));
    _FixedRateCouponOwner = owner;
    _CouponOwner = owner;
}

Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (CFixedRateCoupon^ copy) : CCoupon(CFixedRateCoupon::typeid)
{
#ifdef HANDLE
	_phFixedRateCoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFixedRateCoupon = new boost::shared_ptr<QuantLib::FixedRateCoupon> (copy->GetShared());
        _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppFixedRateCoupon));
    }
}
Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (System::Type^ t) : CCoupon(CFixedRateCoupon::typeid)
{
#ifdef HANDLE
	_phFixedRateCoupon = NULL;
#endif
	if (!t->IsSubclassOf(CFixedRateCoupon::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (QuantLib::Handle<QuantLib::FixedRateCoupon>& childNative, Object^ owner)  : CCoupon(CFixedRateCoupon::typeid)
{
	_phFixedRateCoupon = &childNative;
	_ppFixedRateCoupon = &static_cast<boost::shared_ptr<QuantLib::FixedRateCoupon>>(childNative.currentLink());
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppFixedRateCoupon));
    _FixedRateCouponOwner = owner;
}
Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (QuantLib::Handle<QuantLib::FixedRateCoupon> childNative)  : CCoupon(CFixedRateCoupon::typeid)
{
	_phFixedRateCoupon = &childNative;
	_ppFixedRateCoupon = &static_cast<boost::shared_ptr<QuantLib::FixedRateCoupon>>(childNative.currentLink());
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppFixedRateCoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CFixedRateCoupon::CFixedRateCoupon (QuantLib::FixedRateCoupon childNative)  : CCoupon(CFixedRateCoupon::typeid)
{
#ifdef HANDLE
	_phFixedRateCoupon = NULL;
#endif
	_ppFixedRateCoupon = new boost::shared_ptr<QuantLib::FixedRateCoupon> (new QuantLib::FixedRateCoupon (childNative));
    _ppCoupon = new boost::shared_ptr<QuantLib::Coupon> (boost::dynamic_pointer_cast<QuantLib::Coupon> (*_ppFixedRateCoupon));
}
#endif

Cephei::QL::Cashflows::CFixedRateCoupon::~CFixedRateCoupon ()
{
    if (_ppFixedRateCoupon != NULL)
    {
	    delete _ppFixedRateCoupon;
        _ppFixedRateCoupon = NULL;
    }
}
Cephei::QL::Cashflows::CFixedRateCoupon::!CFixedRateCoupon ()
{
    if (_ppFixedRateCoupon != NULL)
    {
	    delete _ppFixedRateCoupon;
    }
}
QuantLib::FixedRateCoupon& Cephei::QL::Cashflows::CFixedRateCoupon::GetReference ()
{
    if (_ppFixedRateCoupon == NULL) throw gcnew NativeNullException ();
	return **_ppFixedRateCoupon;
}
boost::shared_ptr<QuantLib::FixedRateCoupon>& Cephei::QL::Cashflows::CFixedRateCoupon::GetShared ()
{
    if (_ppFixedRateCoupon == NULL) throw gcnew NativeNullException ();
	return *_ppFixedRateCoupon;
}
QuantLib::FixedRateCoupon* Cephei::QL::Cashflows::CFixedRateCoupon::GetPointer ()
{
    if (_ppFixedRateCoupon == NULL) throw gcnew NativeNullException ();
	return &**_ppFixedRateCoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FixedRateCoupon>& Cephei::QL::Cashflows::CFixedRateCoupon::GetHandle ()
{
	if (_phFixedRateCoupon == NULL)
	{
		_phFixedRateCoupon = new Handle<QuantLib::FixedRateCoupon> (*_ppFixedRateCoupon);
	}
	return *_phFixedRateCoupon;
}
#endif
bool Cephei::QL::Cashflows::CFixedRateCoupon::HasNative () 
{
	return (_ppFixedRateCoupon != NULL);
}

Double Cephei::QL::Cashflows::CFixedRateCoupon::AccruedAmount (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFixedRateCoupon)->accruedAmount ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFixedRateCoupon::Amount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFixedRateCoupon)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Cashflows::CFixedRateCoupon::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppFixedRateCoupon)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::Cashflows::CFixedRateCoupon::InterestRate::get ()
{
    try
    {
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppFixedRateCoupon)->interestRate ( );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFixedRateCoupon::Rate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppFixedRateCoupon)->rate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IFixedRateCoupon^ Cephei::QL::Cashflows::CFixedRateCoupon_Factory::Create (DateTime paymentDate, Double nominal, Cephei::QL::IInterestRate^ interestRate, DateTime accrualStartDate, DateTime accrualEndDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd)
{
    return gcnew CFixedRateCoupon ( paymentDate,  nominal,  interestRate,  accrualStartDate,  accrualEndDate,  refPeriodStart,  refPeriodEnd);
}
Cephei::QL::Cashflows::IFixedRateCoupon^ Cephei::QL::Cashflows::CFixedRateCoupon_Factory::Create (DateTime paymentDate, Double nominal, Double rate, Cephei::QL::Times::IDayCounter^ dayCounter, DateTime accrualStartDate, DateTime accrualEndDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd)
{
    return gcnew CFixedRateCoupon ( paymentDate,  nominal,  rate,  dayCounter,  accrualStartDate,  accrualEndDate,  refPeriodStart,  refPeriodEnd);
}
